# include "newwood.h"

int grid_w[3][3]= {{0,0,0},{0,0,0},{0,0,0}};
int hi;

void delay_w(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

void winmessage(int winner, SDL_Surface *screenu)
{
    SDL_Surface *mark ;
    SDL_Surface *marky ;
    SDL_Rect destmark,srcmark;
    SDL_Event eventh;

    switch(winner)
    {
    case 1:
        mark = SDL_LoadBMP("img/wood/xwin.bmp");
        break;

    case 2:
        mark = SDL_LoadBMP("img/wood/owin.bmp");
        break;

    case 3:
        mark = SDL_LoadBMP("img/wood/tie.bmp");
        break;

    }
    marky = SDL_DisplayFormat(mark);
    srcmark.x = 0;
    srcmark.y = 0;
    srcmark.w = marky->w;
    srcmark.h = marky->h;
    destmark.w = marky->w;
    destmark.h = marky->h;
    destmark.x=0;
    destmark.y=0;
    SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
    SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    SDL_Flip(screenu);


    while(1)
    {
        while (SDL_PollEvent(&eventh))
        {
            switch (eventh.type)
            {
            case SDL_QUIT:
                newgame(screenu);
            case SDL_KEYDOWN:
            {
                if ((eventh.key.keysym.sym == SDLK_ESCAPE)||(eventh.key.keysym.sym == SDLK_KP_ENTER)||(eventh.key.keysym.sym == SDLK_RETURN))
                {
                    newgame(screenu);
                }
            }
            }
        }
    }







}

void checkwin_w(int grid_w[3][3],SDL_Surface *screenu)
{
    int i;
    int win;
    for(i=0; i<3; i++)
    {
        if(grid_w[i][0]==1 && grid_w[i][1]==1 && grid_w[i][2]==1)
        {
            win=1;
            winmessage(win,screenu);
        }
        else if(grid_w[i][0]==2 && grid_w[i][1]==2 && grid_w[i][2]==2)
        {
            win=2;
            winmessage(win,screenu);
        }
    }
    for(i=0; i<3; i++)
    {
        if(grid_w[0][i]==1 && grid_w[1][i]==1 && grid_w[2][i]==1)
        {
            win=1;
            winmessage(win,screenu);
        }
        else if(grid_w[0][i]==2 && grid_w[1][i]==2 && grid_w[2][i]==2)
        {
            win=2;
            winmessage(win,screenu);
        }
    }
    if(grid_w[0][0]==1 && grid_w[1][1]==1 && grid_w[2][2]==1)
    {
        win=1;
        winmessage(win,screenu);
    }
    else if(grid_w[0][0]==2 && grid_w[1][1]==2 && grid_w[2][2]==2)
    {
        win=2;
        winmessage(win,screenu);
    }
    else if(grid_w[0][2]==1 && grid_w[1][1]==1 && grid_w[2][0]==1)
    {
        win=1;
        winmessage(win,screenu);
    }
    else if(grid_w[0][2]==2 && grid_w[1][1]==2 && grid_w[2][0]==2)
    {
        win=2;
        winmessage(win,screenu);
    }

}


void checkgrid_w(int grid_w[3][3],SDL_Surface *screenu)
{
    SDL_Surface *mark ;
    SDL_Surface *marky ;
    SDL_Rect destmark,srcmark;


    mark = SDL_LoadBMP("img/wood/o.bmp");

//1x
    if (grid_w[0][0]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1o
    else if (grid_w[0][0]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }

//2x
    if (grid_w[0][1]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//2o
    else if (grid_w[0][1]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//3x
    if (grid_w[0][2]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//3o
    else if (grid_w[0][2]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=327;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//4x
    if (grid_w[1][0]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//4o
    else if (grid_w[1][0]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//5x
    if (grid_w[1][1]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//5o
    else if (grid_w[1][1]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//6x
    if (grid_w[1][2]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//6o
    else if (grid_w[1][2]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=415;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1x
    if (grid_w[2][0]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1o
    else if (grid_w[2][0]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=149;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1x
    if (grid_w[2][1]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1o
    else if (grid_w[2][1]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=237;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1x
    if (grid_w[2][2]==1)
    {
        mark = SDL_LoadBMP("img/wood/x.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }
//1o
    else if (grid_w[2][2]==2)
    {
        mark = SDL_LoadBMP("img/wood/o.bmp");
        marky = SDL_DisplayFormat(mark);
        srcmark.x = 0;
        srcmark.y = 0;
        srcmark.w = marky->w;
        srcmark.h = marky->h;
        destmark.w = marky->w;
        destmark.h = marky->h;
        destmark.x=503;
        destmark.y=325;
        SDL_SetColorKey(marky, SDL_SRCCOLORKEY, SDL_MapRGB(marky->format, 0, 255, 255));
        SDL_BlitSurface(marky, &srcmark, screenu, &destmark);
    }

//animatefeather();

    int i,j,ij=0;
    for (i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            if (grid_w[i][j]==0)
            {
                ij=1;
            }
        }
    }

    if (ij==0)
    {
        winmessage(3,screenu);
    }

    checkwin_w(grid_w,screenu);



    // Mix_Music *music = NULL;
    // Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);



}
void drawitall_w(int player,int grid_w[3][3],SDL_Surface *screenu)
{
    SDL_Surface *bgu = SDL_LoadBMP("img/wood/w6.bmp");
    SDL_Surface *imageu = SDL_DisplayFormat(bgu);
    SDL_Rect destu,srcu;


    destu.x = 0;
    destu.y = 0;
    srcu.x = 0;
    srcu.y = 0;
    srcu.w = imageu->w;
    srcu.h = imageu->h;
    destu.w = imageu->w;
    destu.h = imageu->h;

    SDL_FillRect( screenu, 0, 0 );
    SDL_BlitSurface(imageu, &srcu, screenu, &destu);


    checkgrid_w(grid_w,screenu);

}

void animgrid_w(SDL_Surface *screenu)
{
    SDL_Surface *bgu = SDL_LoadBMP("img/wood/w0.bmp");
    SDL_Surface *imageu = SDL_DisplayFormat(bgu);

    SDL_Rect destu,srcu;

    int i=-1;

    while(i<6)//13
    {
        i++;
        delay_w(300);

        switch (i)
        {
        case 0 :
            bgu = SDL_LoadBMP("img/wood/w0.bmp");
            break;
        case 1 :
            bgu = SDL_LoadBMP("img/wood/w1.bmp");
            break;
        case 2 :
            bgu = SDL_LoadBMP("img/wood/w2.bmp");
            break;
        case 3 :
            bgu = SDL_LoadBMP("img/wood/w3.bmp");
            break;
        case 4 :
            bgu = SDL_LoadBMP("img/wood/w4.bmp");
            break;
        case 5 :
            bgu = SDL_LoadBMP("img/wood/w5.bmp");
            break;
        case 6 :
            bgu = SDL_LoadBMP("img/wood/w6.bmp");
            break;
//case 7 : bgu = SDL_LoadBMP("img/wood/g7.bmp");break;
//case 8 : bgu = SDL_LoadBMP("img/wood/g8.bmp");break;
//case 9 : bgu = SDL_LoadBMP("img/wood/g9.bmp");break;
//case 10 : bgu = SDL_LoadBMP("img/wood/g10.bmp");break;
//case 11 : bgu = SDL_LoadBMP("img/wood/g11.bmp");break;
//case 12 : bgu = SDL_LoadBMP("img/wood/g12.bmp");break;
        }
        imageu = SDL_DisplayFormat(bgu);

        destu.x = 0;
        destu.y = 0;
        srcu.x = 0;
        srcu.y = 0;
        srcu.w = imageu->w;
        srcu.h = imageu->h;
        destu.w = imageu->w;
        destu.h = imageu->h;

        SDL_BlitSurface(imageu, &srcu, screenu, &destu);

//Flip the backbuffer to the primary Hardware Video Memory
        SDL_Flip(screenu);

    }
    delay_w(100);
}

void movepiece(SDL_Surface *screenu,int player,int picez)//(screenu,player)
{
    SDL_Surface *slcu;
    switch(player)
    {
    case 1 :
        slcu = SDL_LoadBMP("img/wood/handx.bmp");
        break;
    case 2 :
        slcu = SDL_LoadBMP("img/wood/hando.bmp");
        break;
    }
    SDL_Surface *selectu = SDL_DisplayFormat(slcu);
    SDL_Rect desto,srco;
    SDL_SetColorKey( selectu, SDL_SRCCOLORKEY, SDL_MapRGB(selectu->format, 0, 255, 255) );
    SDL_Event event;
    desto.x = 327;
    desto.y = 149;
    srco.x = 0;
    srco.y = 0;
    srco.w = selectu->w;
    srco.h = selectu->h;
    desto.w = selectu->w;
    desto.h = selectu->h;
    int done = 1;
    int anso;
    int xe=0,ye=0,desy=149;
    while (done)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = 0;
                break;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = 0;
                }
                else if (event.key.keysym.sym == SDLK_DOWN)
                {
                    if(ye==0)
                    {
                        desto.y = 237;
                        ye++;
                        desy=237;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(ye==1)
                    {
                        desto.y = 325;
                        ye++;
                        desy=325;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(ye==2)
                    {
                        desto.y = 149;
                        ye=1;
                        desy=149;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                }
                else if (event.key.keysym.sym == SDLK_UP)
                {
                    if(ye==0)
                    {
                        desto.y = 325;
                        ye=3;
                        desy=325;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(ye==1)
                    {
                        desto.y = 149;
                        ye--;
                        desy=149;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(ye==2)
                    {
                        desto.y = 237;
                        ye--;
                        desy=237;
                        drawitall_w(player,grid_w,screenu);
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                }
                else if (event.key.keysym.sym == SDLK_LEFT)
                {
                    if(xe==0)
                    {
                        desto.x = 503;
                        xe=3;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(xe==1)
                    {
                        desto.x = 327;
                        xe--;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(xe==2)
                    {
                        desto.x = 415;
                        xe--;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                }

                else if (event.key.keysym.sym == SDLK_RIGHT)
                {
                    if(xe==0)
                    {
                        desto.x = 415;
                        xe++;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(xe==1)
                    {
                        desto.x = 503;
                        xe++;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                    else if(xe==2)
                    {
                        desto.x = 327;
                        xe=1;
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_BlitSurface(selectu, &srco, screenu, &desto);
                    }
                }
                else if ((event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER))
                {
                    if((grid_w[xe][ye]==0)&&(picez<6))
                    {
                        grid_w[xe][ye]=player;
                        picez++;
                        if(player==1)
                        {
                            player=2;
                        }
                        else if(player==2)
                        {
                            player=1;
                        }
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_Flip(screenu);
                        showpiece(screenu,player,picez);
                    }

                    else if((picez>6)&&(grid_w[xe][ye]==player))
                    {
                        //selected();
                        if(player==1)
                        {
                            player=2;
                        }
                        else if(player==2)
                        {
                            player=1;
                        }
                        drawitall_w(player,grid_w,screenu);
                        desto.y=desy;
                        SDL_Flip(screenu);
                        showpiece(screenu,player,picez);
                    }









                }
            }
            SDL_Flip(screenu);
            break;
            }
        }
    }//
    scanf("%d",&anso);
}

void showpiece(SDL_Surface *screenu,int player,int picez)//(screenu,player)
{
    SDL_Surface *slcu;
    switch(player)
    {
    case 1 :
        slcu = SDL_LoadBMP("img/wood/handx.bmp");
        break;
    case 2 :
        slcu = SDL_LoadBMP("img/wood/hando.bmp");
        break;
    }
    SDL_Surface *selectu = SDL_DisplayFormat(slcu);
    SDL_Rect desto,srco;
    SDL_SetColorKey( selectu, SDL_SRCCOLORKEY, SDL_MapRGB(selectu->format, 0, 255, 255) );
    desto.x = 327;
    desto.y = 149;
    srco.x = 0;
    srco.y = 0;
    srco.w = selectu->w;
    srco.h = selectu->h;
    desto.w = selectu->w;
    desto.h = selectu->h;
    SDL_BlitSurface(selectu, &srco, screenu, &desto);
    SDL_Flip(screenu);
    movepiece(screenu,player,picez);
}

void newwood(SDL_Surface *screenu)
{
    int i,j;
    int picez=0;
    for (i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            grid_w[i][j]=0;
        }
    }
    int player=1;
    animgrid_w(screenu);
    showpiece(screenu,player,picez);
}
